package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.ShoppingCartVo;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param dto
     */
    @Override
    public void insertCart(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        //查询一下购物车内是否有改菜品或者套餐
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectCartById(shoppingCart);
        if (!CollectionUtils.isEmpty(shoppingCarts)) {
            ShoppingCart cart = shoppingCarts.get(0);
            //不是空的，就要将number+1
            cart.setNumber(cart.getNumber() + 1);
            //修改购物车中的number
            shoppingCartMapper.updateNumber(cart);
        } else {
            //如果是空的，那么就需要将该物品添加到购物车
            shoppingCart.setNumber(1);
            //获取用户id
            Long userId = BaseContext.getCurrentId();
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                //本次添加的是菜品
                //根据菜品id查询菜品信息
                DishVO dishVO = dishMapper.queryDishInfoById(dishId);
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setAmount(dishVO.getPrice());
            } else {
                //本次添加的是套餐
                Long setmealId = shoppingCart.getSetmealId();
                //根据套餐id查询套餐信息
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCartMapper.insertCart(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCartVo> getList() {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCartVo> shoppingCartVoList = setmealMapper.getByUserId(userId);
        return shoppingCartVoList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        //获取用户ID
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.cleanShoppingCart(userId);
    }
}
